package com.dwxt.community.controller.supervisor;

import com.dwxt.common.annotation.ValidateFiled;
import com.dwxt.common.annotation.ValidateGroup;
import com.dwxt.common.base.BaseResult;
import com.dwxt.common.base.StatusCode;
import com.dwxt.common.base.YesOrNo;
import com.dwxt.common.util.JwtUtils;
import com.dwxt.community.dao.TrashDeliverMaxMapper;
import com.dwxt.community.entity.TrashDeliverMax;
import com.dwxt.community.entity.TrashIntegralRecoad;
import com.dwxt.community.entity.vo.SupervisorUserVo;
import com.dwxt.community.service.IntegralService;
import com.dwxt.community.service.supervisor.SupervisorUserService;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @program: intelligentcity
 * @description: 巡检员控制层
 * @create: 2020/04/17
 */
@RestController
@RequestMapping("/cms/supervisor")
public class SupervisorUserController {

    @Autowired
    private SupervisorUserService supervisorUserService;
    @Autowired
    private IntegralService integralService;
    @Autowired
    private TrashDeliverMaxMapper trashDeliverMaxMapper;

    /**
     * showdoc
     *
     * @param phone    必须 String 手机号
     * @param password 必须 String 密码
     * @return {"status":200,"msg":"SUCCESS","data":{"user":{"id":"158711800317392","phone":"15922227777","username":"督导员","workId":"1234","roleId":"158711800303859","idCard":"110101199003075373","createTime":1587147330000,"updateTime":1587146897000,"superiorId":"158711800317312","isDudaoyuan":1,"isXunjianyuan":0,"sumUpDoorPropaganda":200},"token":"eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiIxNTg3MTc1MzI4Mzg1MjYiLCJpYXQiOjE1ODcxNzUzMjgsInN1YiI6IjE1ODcxMTgwMDMxNzM5MiIsImV4cCI6MTU4Nzc4MDEyOH0._ffZkXP3oaVzASLoHUzIEOCd8x5L-84DaPlOegZzZKM"}}
     * @catalog 社区/督导员-巡检员
     * @title 登录
     * @method post
     * @description 登录
     * @url /cms/supervisor/login
     */
    @RequestMapping("/login")
    @ValidateGroup(fileds = {
            @ValidateFiled(index = 0, filedName = "phone", regStr = "^1[3|4|5|6|7|8|9][0-9]\\d{4,8}$"),
            @ValidateFiled(index = 0, filedName = "password", notNull = true)})
    public BaseResult login(SupervisorUserVo supervisorUser,boolean status) {

        supervisorUser = supervisorUserService.login(supervisorUser.getPhone(), supervisorUser.getPassword());
        if (supervisorUser != null) {
            if (status && "-1".equals(supervisorUser.getSuperiorId())){
                return BaseResult.build(StatusCode.USER_NOT_EXIST);
            }
            if (supervisorUser.getUseing() == YesOrNo.NO){
                return BaseResult.build(StatusCode.ACCOUNT_STOP);
            }
            supervisorUser.setPassword(null);
            String token = JwtUtils.createJWT(supervisorUser.getId() + ",back", (long) 7 * 24 * 60 * 60 * 1000);
            Map map = new HashMap();
            map.put("token", token);
            map.put("user", supervisorUser);
            return BaseResult.ok(map);
        }else {
            return BaseResult.build(StatusCode.USERNAME_PASSWORD_WRONG);
        }
    }

    /**
     * showdoc
     *
     * @param id 必须 String 户主ID
     * @return {"status":200,"msg":"SUCCESS","data":null}
     * @catalog 社区/督导员-巡检员
     * @title 验证户主
     * @method post
     * @description 验证户主
     * @url /cms/supervisor/verifyOwner
     */
    @RequestMapping("/verifyOwner")
    public BaseResult verifyOwner(String id) {
        if (StringUtils.isBlank(id)) {
            return BaseResult.build(StatusCode.PARAMETER_WRONG);
        }
        return supervisorUserService.verifyOwner(id);
    }

    /**
     * showdoc
     *
     * @param delivererId 必须 String 投递者ID
     * @param trashType 必须 Integer 垃圾类型
     * @return {"status":200,"msg":"SUCCESS","data":null}
     * @catalog 社区/督导员-app用户扫码投放
     * @title app用户扫码投放
     * @method post
     * @description app用户扫码投放
     * @url /cms/supervisor/addIntegralForUser
     */
    @RequestMapping("/addIntegralForUser")
    public BaseResult addIntegralForUser(TrashIntegralRecoad trashIntegralRecoad) {
        if (StringUtils.isBlank(trashIntegralRecoad.getDelivererId()) || ObjectUtils.isEmpty(trashIntegralRecoad.getTrashType())) {
            return BaseResult.build(StatusCode.PARAMETER_WRONG);
        }
        trashIntegralRecoad.setDeliverWeight(YesOrNo.YES);
        trashIntegralRecoad.setCreateTime(new Date());
        int res = integralService.addIntegral(trashIntegralRecoad);
        return res > 0 ? BaseResult.ok() : BaseResult.build(StatusCode.INTEGRAL_ADD_FAILED);
    }

    @RequestMapping("/getTrashTypes")
    public BaseResult getTrashTypes() {
        return BaseResult.ok(trashDeliverMaxMapper.selectAllIncludeIntegralNum());
    }
    /**
     * showdoc
     *
     * @param id 必须 String 二维码识别结果
     * @return {"status":200,"msg":"SUCCESS","data":{"id":"158502014539730","phone":"15123058496","identityCard":"513030199201088419","houseNumber":"10栋32-6","communityName":"聚金万佳苑","lastGotBagTime":"2020-04-18 17:01:50"}}
     * @catalog 社区/督导员-巡检员
     * @title 扫码获取用户信息
     * @method get
     * @description 扫码获取用户信息
     * @url /cms/supervisor/getUser
     */
    @RequestMapping("/getUser")
    public BaseResult getUser(String id) {
        if (StringUtils.isBlank(id)) {
            return BaseResult.build(StatusCode.PARAMETER_WRONG);
        }
        return supervisorUserService.getUser(id);
    }

    /**
     * showdoc
     *
     * @return {"status":200,"msg":"SUCCESS","data":{"total":1,"list":[{"id":"158711800317392","phone":"15922227777","username":"督导员","workId":"1234","password":"E10ADC3949BA59ABBE56E057F20F883E","roleId":"158711800303859","idCard":"110101199003075373","createTime":1587147330000,"updateTime":1587146897000,"superiorId":"158711800317312","isDudaoyuan":1,"isXunjianyuan":0,"sumUpDoorPropaganda":200,"num":1}],"pageNum":1,"pageSize":10,"size":1,"startRow":1,"endRow":1,"pages":1,"prePage":0,"nextPage":0,"isFirstPage":true,"isLastPage":true,"hasPreviousPage":false,"hasNextPage":false,"navigatePages":8,"navigatepageNums":[1],"navigateFirstPage":1,"navigateLastPage":1}}
     * @catalog 社区/督导员-巡检员
     * @title 督导员列表
     * @method get
     * @description 督导员列表
     * @url /cms/supervisor/getCensors/xunjian
     */
    @RequestMapping("/getCensors/xunjian")
    public BaseResult getCensors(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize) {
        return supervisorUserService.getCensors(pageNum,pageSize);
    }
}
